<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2018/8/24
 * Time: 15:24
 */
namespace app\withdraw;

use app\common\Common;
use app\data\ShopData;
use app\data\UserData;
use app\data\WithdrawData;
use think\Db;

class WithdrawApplication extends Base
{
    /**
     * 提现
     * @param $user_open_id
     * @param $money
     * @param $type
     * @return \think\response\Json
     */
    public function addWithdraw($user_open_id, $money,$type=1)
    {
        if (Common::checkTableFields('drh_user_info', 'user_open_id', $user_open_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '用户不存在']);
        }
        if($type==1){
            return $this->addWithdrawShop($user_open_id,$money);
        }else if($type ==2){
            return $this->addWithdrawUser($user_open_id,$money);
        }else{
            return json(['code'=>$this->f_code,'msg'=>'没有提现权限']);
        }
    }

    /**
     * 用户提现
     * @param $user_open_id
     * @param $money
     * @return \think\response\Json
     */
    private function addWithdrawUser($user_open_id,$money)
    {
        $user = new UserData();
        $userInfo = $user->getInfo($user_open_id, ['user_integral', 'user_money', 'id']);
        $user_money = $userInfo['user_money'];//用户初始金钱 f
        if ($money < 0 || $user_money < $money || !Common::checkFloat($money, 100)) {
            return json(['code' => $this->f_code, 'msg' => '提现金钱输入错误']);
        }
        $withdraw=new WithdrawData();
        $user_id = $userInfo['id'];
        $user_integral = $userInfo['user_integral'];//用户初始积分
        $time = time();
        $order_num = Common::getOrderNumber('t', 10, $time);//单号
        Db::startTrans();
        $fees_proportion = 0;//empty(session('withdraw')) ? Common::getConfig('withdraw') : session('withdraw'); 不收手续费
        $fees = 0;//floor(($fees_proportion / 100) * $money)  不收手续费
        try {
            $withdraw->addWithdraw($user_id, $order_num, $money, $time, $money - $fees, $fees, $fees_proportion,2);
            $user->addUserFlow($user_id, $user_integral, $user_money, $time, '-' . $money, 0,
                $order_num, 3, $user_money - $money, $user_integral, $fees);
            $user->changIntegralMoney($user_id, $user_integral, $user_money - $money);
            Db::commit();
            return json(['code' => $this->t_code, 'msg' => '提现成功 请等待审核']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 商户提现
     * @param $user_open_id
     * @param $money
     * @return \think\response\Json
     */
    private function addWithdrawShop($user_open_id,$money)
    {
        $shop=new ShopData();
        $shopInfo=$shop->getInfo($user_open_id,['a.money','a.id','a.user_id','a.integral'],'b.user_open_id');
        $shop_money = $shopInfo['money'];//商户初始金钱
        $shop_id=$shopInfo['id'];
        $shop_integral = $shopInfo['integral'];//商户初始积分
        if ($money < 0 || $shop_money < $money || !Common::checkFloat($money, 100)) {
            return json(['code' => $this->f_code, 'msg' => '提现金钱输入错误']);
        }
        $withdraw=new WithdrawData();
        $user_id = $shopInfo['user_id'];
        $time = time();
        $order_num = Common::getOrderNumber('t', 10, $time);//单号
        $fees_proportion = 0;//empty(session('withdraw')) ? Common::getConfig('withdraw') : session('withdraw'); 不收手续费
        $fees = 0;//floor(($fees_proportion / 100) * $money)  不收手续费
        try {
            $withdraw->addWithdraw($user_id, $order_num, $money, $time, $money - $fees, $fees, $fees_proportion,1);
            if(!$shop->shopFlow($order_num,$time,$shopInfo,3,'-'.$money,0,$fees)){
                Db::rollback();
                return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
            }
            $shop->changIntegralMoney($shop_id, $shop_integral, $shop_money-$money);
            Db::commit();
            return json(['code' => $this->t_code, 'msg' => '提现成功 请等待审核']);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => $e->getMessage()]);
        }
    }
}